var express = require('express');
var router = express.Router();
var mongoose = require("mongoose");


var settings = require('../settings');

var host = settings.host;
var db = 'test7';

//sort helper
function sortNum(a,b){
	return a.times - b.times;
}

//open db
mongoose.connect('mongodb://' + host + '/' + db);



var Student = require('../modules/schema.js').Student;
var Teacher = require('../modules/schema.js').Teacher;
var Course = require('../modules/schema.js').Course ;
var Exam = require('../modules/schema.js').Exam;
var Case_info = require('../modules/schema.js').Case_info;
var Learn_info = require('../modules/schema.js').Learn_info;
var Job_info = require('../modules/schema.js').Job_info;

//'admin'--to add data and manage data
router.get('/admin',function(req,res){
	res.render('admin/admin',{title:'欢迎管理员'});
});


/* * * * * * * * * * * * * * * * * * * * *
 *                                       *
 * student admin                         *
 * need a page effect                    *
 * * * * * * * * * * * * * * * * * * * * */
//get count with mongoose?


router.get('/admin/student/page_:page',function(req,res){
	var page = parseInt(req.param('page'));
	Student.count({},function(err,count){
		if(err) console.log(err);
		Student.find('*').sort({ field: 'asc', sid: 1 })
		.skip((page - 1) * 10)
		.limit(10)
		.exec(function(err,students){
			if(err) console.log(err);
			res.render('admin/student/index',{students:students,total_page:Math.ceil(count/10)});
		});
	});
});

router.post('/admin/student/qurey',function(req,res){
	if(req.body.qurey_type === "sid"){
		Student.find({sid:req.body.qurey_info}).exec(function(err,students){
			if(err) console.log(err);
			res.render('admin/student/index',{students:students});
		});
	}else{
		Student.find({name:req.body.qurey_info}).exec(function(err,students){
			if(err) console.log(err);
			res.render('admin/student/index',{students:students});
		});
	}
});


//add student without course
router.post('/admin/student/add',function(req,res){
	var student = new Student({
		sid:req.body.id,
		name:req.body.name
	});	
	student.save(function(err){
		if(err) console.log(err);
		res.redirect('/admin/student/page_1');
	});
});

/* * * * * * * * * * * * * * * * * * * * * * * * * *
 * update the attendance info of every sutdent     *
 * for every course                                *
 *                                                 *
 *                                                 *
 * * * * * * * * * * * * * * * * * * * * * * * * * */


router.post('/admin/student/attendance',function(req,res){
	Student.findOne({sid:req.body.sid},function(err,student){
		if(err) console.log(err);
		if(student){
			var course = student.Course;
			var attendance = [];
			var update_cheak;
			for(var i in course){
				if(course[i].Course_id == req.body.Course_id){
					update_cheak = i;
				}
			}

			if(update_cheak != undefined){
				attendance = course[update_cheak].Attendance;
				var update_cheak_2;
				for(var i in attendance){
					if(attendance[i].times == req.body.times){
						update_cheak_2 = i;
						break;
					}
				}
				if(update_cheak_2 != undefined){
					attendance[update_cheak_2].here = req.body.here;
				}else{
					attendance.push({times:req.body.times,here:req.body.here});
					attendance.sort(sortNum);
				}
				course[update_cheak].Attendance = attendance;                              //mark:this line may not be needed...
			}else{
				if(req.body.here === "true"){
					attendance.push({times:req.body.times,here:true});
				}else{
					attendance.push({times:req.body.times,here:false});
				}	
				course.push({Course_id:req.body.Course_id,Attendance:attendance});
			}
			student.Course = course;
			student.markModified('array');
			student.save(function(err){
				if(err) {console.log(student);return console.log(err);}
				res.redirect('/admin/student/page_1');
			});
		}else{
			res.redirect('/admin/student');
		}
	});
});

/* * * * * * * * * * * * * * * * * * * * * * * * * * * *  
 *                                                     *
 *  admin teacher                                      *
 *                                                     *
 *                                                     *
 *                                                     *
 *  tired                                              *
 *                                                     *
 *                                                     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */

router.get('/admin/teacher/page_:page',function(req,res){
	var page = parseInt(req.param('page'));
	Teacher.count({},function(err,count){
		if(err) console.log(err);
		Teacher.find().skip((page-1)/10).limit(10).exec(function(err,teachers){
			if(err) console.log(err);
			res.render('admin/teacher/index',{teachers:teachers,total_page:Math.ceil(count/10)});	
		});
	});
});






module.exports = router;
